﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using Analysis.DAL;
using Analysis.DAL.Repositories;
using Analysis.Entities;
using Analysis.Code;


namespace Analysis.Account
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginUser.LoggingIn += new LoginCancelEventHandler(LoginUser_LoggingIn);
            LoginUser.LoginError += LoginUser_LoginError;
            LoginUser.Authenticate += LoginUser_Authenticate;

            if (!IsPostBack)
            {
                if (this.Request.Cookies["email"] != null && !string.IsNullOrEmpty(Request.Cookies["email"].Value))
                {
                    this.LoginUser.UserName = Request.Cookies["email"].Value;
                    this.LoginUser.RememberMeSet = true;
                }
                DoRedirectIfLogin();
            }
        }

        private bool TryLogin()
        {
            bool result = false;
            var validateResult = Membership.ValidateUser(this.LoginUser.UserName, this.LoginUser.Password);

            if (validateResult)
            {
                result = true;
            }
            return result;
        }

        private void DoRedirectIfLogin()
        {
            if (User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Admin/ProductList.aspx", endResponse: true);

                if (Roles.IsUserInRole("admin"))
                    Response.Redirect("~/Dashboard.aspx", endResponse: true);
                else
                    Response.Redirect("~/Dashboard.aspx", endResponse: true);
            }
        }

        
        
        private void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
        {
            if (TryLogin())
            {
                if (LoginUser.RememberMeSet)
                {
                    var cookie = new HttpCookie("email", LoginUser.UserName);
                    cookie.Expires = DateTime.Now.AddYears(1);
                    Response.Cookies.Add(cookie);
                }
                else
                {
                    var cookie = new HttpCookie("email", String.Empty);
                    cookie.Expires = DateTime.Now.AddYears(1);
                    Response.Cookies.Add(cookie);
                }

                FormsAuthentication.SetAuthCookie(LoginUser.UserName, LoginUser.RememberMeSet);

                if (Roles.IsUserInRole(LoginUser.UserName, "admin"))
                    Response.Redirect("~/Dashboard.aspx", endResponse: true);
                else
                    Response.Redirect("~/Dashboard.aspx", endResponse: true);
            }

            e.Authenticated = false;
        }

        private void LoginUser_LoginError(object sender, EventArgs e)
        {
            LoginUser.FindControl("lblError").Visible = true;
        }

        void LoginUser_LoggingIn(object sender, LoginCancelEventArgs e)
        {
        }

        public string ServerPath
        {
            get { return Server.MapPath("~/Templates/"); }
        }
    }
}
